package com.xyht.sca_s.student_manage_system.modules.produtProcurement.controller;


import com.xyht.sca_s.student_manage_system.auth.util.JwtTokenUtil;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.common.desEncodeAndDecode.annotation.SecretBody;
import com.xyht.sca_s.student_manage_system.modules.produtProcurement.entity.dto.SmsProductProcurementAllowDto;
import com.xyht.sca_s.student_manage_system.modules.produtProcurement.entity.dto.SmsProductProcurementCancelDto;
import com.xyht.sca_s.student_manage_system.modules.produtProcurement.entity.dto.SmsProductProcurementDto;
import com.xyht.sca_s.student_manage_system.modules.produtProcurement.entity.dto.SmsProductProcurementHistoryReq;
import com.xyht.sca_s.student_manage_system.modules.produtProcurement.service.SmsProductProcurementApprovalService;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.concurrent.Future;

/**
 * <p>
 * 大宗商品审批表 前端控制器
 * </p>
 *
 * @author XYHT
 * @since 2023-05-22
 */
@RestController
@RequestMapping("/produtProcurement")
@SecretBody
public class SmsProductProcurementApprovalController {

    @Resource
    private SmsProductProcurementApprovalService smsProductProcurementApprovalService;
    @Resource
    private JwtTokenUtil jwtTokenUtil;

    @ApiModelProperty(value = "提交大宗商品申请")
    @PostMapping("/add")
    public ResponseResult addProductProcurement(@RequestBody @Validated SmsProductProcurementDto smsProductProcurementDto
            , HttpServletRequest request){
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return smsProductProcurementApprovalService.addProductProcurement(userId,smsProductProcurementDto);
    }

    @ApiModelProperty(value = "撤销申请")
    @PostMapping("/cancel")
    public ResponseResult cancelProductProcurement(HttpServletRequest request, @RequestBody SmsProductProcurementCancelDto cancelDto){
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return smsProductProcurementApprovalService.cancelProductProcurement(userId,cancelDto);
        
    }

    @ApiModelProperty(value = "审批")
    @PostMapping("/allow")
    public ResponseResult allowProductProcurement(HttpServletRequest request, @RequestBody SmsProductProcurementAllowDto allowDto){
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return smsProductProcurementApprovalService.allowProductProcurement(userId,allowDto);
    }

    @ApiModelProperty(value = "查看审批详情")
    @GetMapping("/detail")
    public ResponseResult getProductProcurementDetail(HttpServletRequest request,
                                                      @RequestParam(value = "examineId",required = false)String examineId){
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return smsProductProcurementApprovalService.getProductProcurementDetail(userId,examineId);
    }

    @ApiModelProperty(value = "查看审批列表")
    @GetMapping("/list")
    public ResponseResult getProductProcurementList(@RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                    @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                    @RequestParam(value = "examine_type",required = false) Integer examine_type,
                                                    @RequestParam(value = "search_str",required = false) String search_str,
                                                    HttpServletRequest request){
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return smsProductProcurementApprovalService.getProductProcurementList(userId,examine_type,pageNum,pageSize,search_str);
    }

    @ApiModelProperty(value = "申请人指定流程（申请前获取）")
    @GetMapping("/processList")
    public ResponseResult getProductOriginSettingProcess(){
        return smsProductProcurementApprovalService.getProductOriginSettingProcess();
    }


    @ApiModelProperty(value = "申请表下载")
    @GetMapping("/download")
    public ResponseResult tableDownload(@RequestParam(value = "apply_id") String apply_id, HttpServletResponse response) {
        ResponseResult responseResult;
        try{
            Future<ResponseResult> future = smsProductProcurementApprovalService.tableDownload(apply_id, response);
            while (true){
                if (future.isDone()){
                    break;
                }
            }
            responseResult = future.get();
        }catch (Exception e){
            e.printStackTrace();
            responseResult = CommonResult.failed(CommonCodeEnum.FAIL);
        }
        return responseResult;

    }


    @ApiModelProperty("查看所有的记录")
    @GetMapping("/allList")
    public ResponseResult getAllProductProcurementList( @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                        @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                        @RequestParam(value = "allowStatus",required = false) Integer allowStatus,
                                                        @RequestParam(value = "applyName",required = false) String applyName,
                                                        @RequestParam(value = "applyDepartment",required = false) String applyDepartment){
        return smsProductProcurementApprovalService.getAllProductProcurementList(allowStatus,applyName,applyDepartment,pageSize,pageNum);
    }

    @ApiModelProperty(value = "导出历史记录")
    @PostMapping("/exportHistory")
    public ResponseResult exportExamineHistoryList(@RequestBody SmsProductProcurementHistoryReq req, HttpServletResponse response) {
        ResponseResult responseResult;
        try{
            Future<ResponseResult> future = smsProductProcurementApprovalService.exportExamineHistoryList(req, response);
            while (true){
                if (future.isDone()){
                    break;
                }
            }
            responseResult = future.get();
        }catch (Exception e){
            e.printStackTrace();
            responseResult = CommonResult.failed(CommonCodeEnum.FAIL);
        }
        return responseResult;
    }

}

